package cn.kduck.code.generate.service.impl;

import cn.kduck.code.generate.CodeGenProgressListener;

import java.util.*;

public class CodeGenProgressListenerImpl implements CodeGenProgressListener {

    private Map<String, List<CodeGenProgress>> codeGenProgressCacheMap = new HashMap();
    @Override
    public void onProgressUpdate(String moduleId,String type, String genFileName) {
        List<CodeGenProgress> codeGenProgressList = codeGenProgressCacheMap.get(moduleId);
        if(codeGenProgressList == null){
            codeGenProgressList = new ArrayList<>();
            codeGenProgressCacheMap.put(moduleId,codeGenProgressList);
        }
        CodeGenProgress codeGenProgress;
        if(codeGenProgressList.isEmpty()){
            codeGenProgress = new CodeGenProgress();
            codeGenProgress.setType(type);
            codeGenProgress.setGenFileName(genFileName);
            codeGenProgressList.add(codeGenProgress);
            codeGenProgress.onProcess();
        }else{
            codeGenProgress = codeGenProgressList.get(codeGenProgressList.size() - 1);
            String type1 = codeGenProgress.getType();
            String genFileName1 = codeGenProgress.getGenFileName();
            if(type1.equals(type) && (genFileName1 == null || genFileName1.equals(genFileName))){
                codeGenProgress.onProcess();
            }else{
                codeGenProgress.onComplete();
                codeGenProgress = new CodeGenProgress();
                codeGenProgress.setType(type);
                codeGenProgress.setGenFileName(genFileName);
                codeGenProgressList.add(codeGenProgress);
                codeGenProgress.onProcess();
            }
        }

    }

    @Override
    public void onGenerationComplete(String moduleId) {
        List<CodeGenProgress> codeGenProgressList = codeGenProgressCacheMap.get(moduleId);
        CodeGenProgress codeGenProgress = codeGenProgressList.get(codeGenProgressList.size() - 1);
        codeGenProgress.onComplete();

        codeGenProgressCacheMap.remove(moduleId);
    }

    @Override
    public void onGenerationFailed(String moduleId,Exception error) {
        codeGenProgressCacheMap.remove(moduleId);
    }

    @Override
    public List<CodeGenProgress> getGenProgress(String moduleId) {
        return codeGenProgressCacheMap.get(moduleId);
    }

    public static class CodeGenProgress{
        private String type;
        private String genFileName;
        private String progressState;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getGenFileName() {
            return genFileName;
        }

        public void setGenFileName(String genFileName) {
            this.genFileName = genFileName;
        }

        public String getProgressState() {
            return progressState;
        }

        public void onProcess(){
            if("COMPLETE".equals(progressState)){
                return;
            }
            if(progressState == null || progressState.length() >= 3){
                progressState = ".";
            }else{
                progressState += ".";
            }
        }

        public void onComplete(){
            progressState = "COMPLETE";
        }
    }
}
